package com.zzusedating.androidapp.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.UserInfoActivity;
import com.zzusedating.androidapp.model.RecommendItem;

import java.util.ArrayList;
import java.util.Locale;

public class RecommendRecAdapter extends RecyclerView.Adapter<RecommendRecAdapter.ViewHolder>{
    public static final String USER_ID_EXTRA = "userIDExtra";
    private ArrayList<RecommendItem> items;
    private Context context;

    public RecommendRecAdapter(ArrayList<RecommendItem> items, Activity activity){
        context = activity;
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.recycler_item_recom, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final int pos = position;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra(USER_ID_EXTRA, items.get(pos).getUserId());
                context.startActivity(intent);
            }
        });
        holder.setAllView(items.get(position));
    }

    @Override
    public int getItemCount() {
        if (items != null)
            return items.size();
        return 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView nameView;
        TextView levelView;
        TextView residentView;
        TextView heightView;
        TextView ageView;
        TextView hobbyView;
        TextView selfDescView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            nameView = itemView.findViewById(R.id.RecyclerItemRec_Name);
            levelView = itemView.findViewById(R.id.RecyclerItemRec_Level);
            residentView = itemView.findViewById(R.id.RecyclerItemRec_Residence);
            heightView = itemView.findViewById(R.id.RecyclerItemRec_Height);
            ageView = itemView.findViewById(R.id.RecyclerItemRec_Age);
            hobbyView = itemView.findViewById(R.id.RecyclerItemRec_Hobby);
            selfDescView = itemView.findViewById(R.id.RecyclerItemRec_SelfDesc);
        }
        void setAllView(RecommendItem item){
            nameView.setText(item.getName());
            levelView.setText(String.format(Locale.CHINA, "Lv.%d", item.getVipLevel()));
            residentView.setText(item.getDomicile());
            heightView.setText(String.format(Locale.CHINA, "%dcm", item.getHeight()));
            ageView.setText(String.format(Locale.CHINA, "%d岁", item.getAge()));
            hobbyView.setText(item.getHobby());
            selfDescView.setText(item.getSelfIntro());
        }
    }
}
